using System;
using System.Linq;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.BusinessLogic.Skill;
using TrainingManagement.api.Models.Data.CRUD;
using TrainingManagement.api.Models.Data.CRUD.TrainingRecord;

namespace TrainingManagement.api.Models.BusinessLogic.TrainingRecord
{
    public class TrainingRecordCreateOperation : BusinessOperation<BusinessObjects.TrainingRecord, BusinessObjects.TrainingRecord>
    {
        readonly TrainingRecordInsertDataSource _recordInsert = new TrainingRecordInsertDataSource();
        readonly SkillSelectOperation _skillSelect = new SkillSelectOperation();

        public override BusinessObjects.TrainingRecord Run(BusinessObjects.TrainingRecord input)
        {
            input.SkillId = input.Skill.SkillId;
            if (_recordInsert.Execute(input))
            {
                input.Skill = _skillSelect.Run(input.Skill.SkillId).FirstOrDefault();
                return input;
            }
            throw new Exception(string.Format("Record failed to save!"));
        }
    }
}